package com.mirror.utils.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.mirror.llk.view.ViewHandler;
import com.mirror.utils.base.BaseApplication;

public class CustomComplexAdapter extends SimpleAdapter {

	private int resource;

	private List<Map<String, ?>> data;

	private Context context;

	private ViewHandler viewHandler = BaseApplication.getViewHandler();

	@SuppressWarnings("unchecked")
	public CustomComplexAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		this.context = context;
		// 缓存布局
		this.resource = resource;
		// 缓存数据
		this.data = (List<Map<String, ?>>) data;
		// 支持图片
		this.setViewBinder(new ViewBinder() {

			public boolean setViewValue(View view, Object data, String textRepresentation) {
				if (view instanceof ImageView && data instanceof Bitmap) {
					((ImageView) view).setImageBitmap((Bitmap) data);
					return true;
				}
				if (view instanceof ImageView && data instanceof Drawable) {
					((ImageView) view).setImageDrawable((Drawable) data);
					return true;
				}
				return false;
			}
		});
	}

	public List<Map<String, ?>> getData() {
		return data;
	}

	public void setData(List<Map<String, ?>> data) {
		this.data = data;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		convertView = super.getView(position, convertView, parent);
		return viewHandler.transfer(context, resource, convertView, data.get(position));
	}

}
